package com.hqyj.hrms.modules.personnel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.personnel.dao.EmployeeMapper;
import com.hqyj.hrms.modules.personnel.entity.AgeCount;
import com.hqyj.hrms.modules.personnel.entity.EduCount;
import com.hqyj.hrms.modules.personnel.entity.Employee;
import com.hqyj.hrms.modules.personnel.entity.SexCount;
import com.hqyj.hrms.modules.personnel.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Autor zyj
 * @Date 2021/9/27
 **/
@Service
public class EmployeeServiceImpl implements EmployeeService {
    //注入mapper
    @Autowired(required = false)
    EmployeeMapper employeeMapper;

    @Override
    public List<Map> getAge() {
        List<AgeCount> list=new ArrayList();
        list=employeeMapper.queryAgeCount();
        List<Map> maps=new ArrayList<Map>();
        for (int i=0;i<list.size();i++){
            String age = list.get(i).getEmpAge();
            Integer count=list.get(i).getCount();
            Map map=new HashMap();
            map.put("name",age);
            map.put("value",count);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Map> getEdu() {
        List<EduCount> list=new ArrayList();
        list=employeeMapper.queryEduCount();
        List<Map> maps=new ArrayList<Map>();
        for (int i=0;i<list.size();i++){
            String edu = list.get(i).getEmpEducation();
            Integer count=list.get(i).getCount();
            Map map=new HashMap();
            map.put("name",edu);
            map.put("value",count);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Map> getSex() {
        List<SexCount> list=new ArrayList();
        list=employeeMapper.querySexCount();
        List<Map> maps=new ArrayList<Map>();
        for (int i=0;i<list.size();i++){
            String sex = list.get(i).getEmpSex();
            Integer count=list.get(i).getCount();
            Map map=new HashMap();
            map.put("name",sex);
            map.put("value",count);
            maps.add(map);
        }
        return maps;
    }

//    @Override
//    public List<SexCount> sexC() {
//        List<SexCount> sexCounts = employeeMapper.querySexCount();
//        return sexCounts;
//    }

    @Override
    public HashMap<String, Object> query(String str1, String str2) {
        HashMap<String, Object> map = new HashMap<String, Object>();
       Map<String, Object> params=new  HashMap<>();
       params.put("emp_department",str1);
       params.put("emp_sex",str2);
//        Employee employee = new Employee();
                QueryWrapper<Employee> wrapper = new QueryWrapper<>();
                wrapper.allEq(params);

        //第一步创建分页对象
        Page<Employee> employeePage = new Page<>(1, 10);
        //第二页  查询分页  获取分页数据集
        Page<Employee> pageList = employeeMapper.selectPage(employeePage, wrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
//        System.out.println(pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
//        List<UserInfo> list = userInfoMapper.selectList(null);
//        map.put("info",list);
        return map;
    }

    @Override
    public HashMap<String, Object> list(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        //创建查询插件对象
//        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//
//        //条件   列名   参数
//        queryWrapper.eq(
//                employee.getEmpName() != null && !employee.getEmpName().equals(""),
//                "emp_name",
//                employee.getEmpName());

        String keyWord = employee.getKeyWord();

        QueryWrapper wrapper = new QueryWrapper<Employee>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_name", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_sex", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_education", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_department", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_job", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_status", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "emp_remarks", keyWord);

        //第一步创建分页对象
        Page<Employee> employeePage = new Page<>(employee.getPage(), employee.getRow());
        //第二页  查询分页  获取分页数据集
        Page<Employee> pageList = employeeMapper.selectPage(employeePage, wrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
//        System.out.println(pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
//        List<UserInfo> list = userInfoMapper.selectList(null);
//        map.put("info",list);
        return map;
    }

    @Override
    public HashMap<String, Object> add(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = employeeMapper.insert(employee);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = employeeMapper.updateById(employee);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = employeeMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }
}
